import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
export default defineConfig(({ mode }) => {
  return {
    server: {
      host: true,
      open: false,
      proxy: {
        "/api": {
          target: "https://baidu1.com",
          changeOrigin: true,
        },
      },
    },
    base: "./",
    build: {
      assetsDir: "assets",
      minify: mode === "production" ? "terser" : false, // 生产环境下启用 Terser 最小化代码
      chunkSizeWarningLimit: 2000,
      manifest: true,
      terserOptions: { //对控制台的打印进行删除
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
      rollupOptions: { //在用到第三方插件的时候会特别有用
        output: {
          // 超大静态资源拆分
          manualChunks(id :any) {
            // console.log(id,'id')
            if (id.includes("node_modules")) {
              return id
                .toString()
                .split("node_modules/")[1]
                .split("/")[0]
                .toString();
            }
          },
          chunkFileNames: "assets/js/[name]-[hash].js",
          entryFileNames: "assets/js/[name]-[hash].js",
          assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
        },
      },
    },
    plugins: [
      vue(),
      AutoImport({
        resolvers: [
          ElementPlusResolver({
            importStyle: "sass",
          }),
        ],
      }),
      Components({
        resolvers: [
          ElementPlusResolver({
            importStyle: "sass",
          }),
        ],
      }),
    ],
    resolve: {
      alias: {
        "@": "/src",
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "/src/assets/style/global/element-plus.scss" as *;`,
        },
      },
    },
  };
});
